package com.cn.hnust.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONArray;
import com.cn.hnust.pojo.Column;
import com.cn.hnust.service.IColumnService;
import com.cn.hnust.util.Page;
import com.cn.hnust.util.Qiniu;
import com.mysql.fabric.Response;

@Controller
@RequestMapping("/column")
public class ColumnController {
	@Autowired
	private IColumnService ColumnService;

	@RequestMapping(value = "/List")
	public String Columnlist(Model model,String pageNow) {
		Page page = null;// new 对象在下面
		int totalCount = ColumnService.getCount();// 获取当前用户总记录条数
		int pageNow1 = Integer.parseInt(pageNow);
		page = new Page(totalCount, pageNow1);// 这样写的好处，判断完成外面可以继续调用
		model.addAttribute("page", page);
		List<Column> column = ColumnService.queryAllRecord(page);
		model.addAttribute("page", page);
		model.addAttribute("column", column);
		return "column";
	}

	@RequestMapping(value = "/del")
	@ResponseBody
	public Map<String, Object> DelColumn(int column_id, Model model) {
		Map<String,Object> resultMap = new HashMap<String, Object>(); 
		if (column_id > 0) {
			ColumnService.delColumn(column_id);
			int pageNow = 1;
			Page page = null;
			int totalCount = ColumnService.getCount();
			page = new Page(totalCount, pageNow);
			List<Column> column = ColumnService.queryAllRecord(page);
			model.addAttribute("page", page);
			model.addAttribute("column", column);
			 resultMap.put("result", "删除成功");  
			  return resultMap;  
			
		   
		}

		return resultMap;
	}

	@RequestMapping(value = "/add")
	public String Column() {

		return "ColumnAdd";
	}

	@RequestMapping(value = "/ColumnAdd")
	public String ColumnAdd(Model model, HttpServletRequest request,String pageNow,@RequestParam("pic") MultipartFile pic) {
		String column_name = request.getParameter("column_name").trim();
		String column_wz = request.getParameter("column_tavern").trim();
		String column_ms = request.getParameter("column_ms").trim();
		String column_jg = request.getParameter("column_jg").trim();
		String column_lx = request.getParameter("column_lx").trim();
		if (!StringUtils.isEmpty(pic)) {
            String path = "c:\\image\\";
            String originalFileName = pic.getOriginalFilename();
            // 新的图片名称
            String newFileName = UUID.randomUUID() + originalFileName.substring(originalFileName.lastIndexOf("."));
            // 新的图片
            String name = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(new Date()); 
            System.out.println(newFileName);
            String extension = FilenameUtils.getExtension(originalFileName);
            File newFile = new File(path + newFileName);
            String updz=path + newFileName;
            //将内存中的数据写入磁盘
           
				try {
					pic.transferTo(newFile);
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
		String pho = null;

		Qiniu qiniuUtils = new Qiniu();
			try {
				pho = qiniuUtils.upload(newFileName, updz);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		Column colum = new Column();
		Page page = null;
		int pageNow1 = Integer.parseInt(pageNow);
		int totalCount = ColumnService.getCount();
		page = new Page(totalCount,pageNow1);
		colum.setColumn_name(column_name);
		colum.setColumn_tavern(column_wz);
		colum.setColumn_tp(pho);
		colum.setColumn_ms(column_ms);
		colum.setColumn_jg(column_jg);
		colum.setColumn_lx(column_lx);
		ColumnService.insert(colum);
		List<Column> column = ColumnService.queryAllRecord(page);
		model.addAttribute("page", page);
		model.addAttribute("column", column);
		return "column";
	}
		return null;
	}
	@RequestMapping(value = "/update")
	public String UpColumn(int column_id, Model model) {
		
			Column column = ColumnService.getUser(column_id);
			model.addAttribute("column", column);
			return "ColumnUp";	
	}
	
	@RequestMapping(value = "/ColumnUp")
	public String ColumnUp(Model model,HttpServletRequest request,int column_id, @RequestParam("pic") MultipartFile pic){
		String column_name = request.getParameter("column_name").trim();
		String column_tavern = request.getParameter("column_tavern").trim();
		String column_ms = request.getParameter("column_ms").trim();
		String column_jg = request.getParameter("column_jg").trim();
		String column_lx = request.getParameter("column_lx").trim();
		int pageNow1 = 1;
		if (!StringUtils.isEmpty(pic)) {
			String path = "c:\\image\\";
			String originalFileName = pic.getOriginalFilename();
			// 新的图片名称
			String newFileName = UUID.randomUUID() + originalFileName.substring(originalFileName.lastIndexOf("."));
			// 新的图片
			String name = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(new Date());
			System.out.println(newFileName);
			String extension = FilenameUtils.getExtension(originalFileName);
			File newFile = new File(path + newFileName);
			String updz = path + newFileName;
			// 将内存中的数据写入磁盘

			try {
				pic.transferTo(newFile);
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			String pho = null;

			Qiniu qiniuUtils = new Qiniu();
			try {
				pho = qiniuUtils.upload(newFileName, updz);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		Column colum = new Column();
		Page page = null;
		int totalCount = ColumnService.getCount();
		page = new Page(totalCount,pageNow1);
		colum.setColumn_id(column_id);
		colum.setColumn_name(column_name);
		colum.setColumn_tavern(column_tavern);
		colum.setColumn_tp(pho);
		colum.setColumn_ms(column_ms);
		colum.setColumn_jg(column_jg);
		colum.setColumn_lx(column_lx);
		ColumnService.updateByPrimaryKey(colum);
		List<Column> column = ColumnService.queryAllRecord(page);
		model.addAttribute("column", column);
		model.addAttribute("page", page);
		return "column";
		}
		return null;
	}
	
	@RequestMapping(value = "/aa")  
    @ResponseBody//此处不能省略 否则ajax无法解析返回值  
    public Map<String,Object> add(String scenic_name,String scenic_jg,String scenic_jj,@RequestParam("pic") MultipartFile pic,
    		String scenic_dq,String textarea1){  
		Map<String,Object> resultMap = new HashMap<String, Object>(); 
		
		 String originalFilename = pic.getOriginalFilename();  
         System.out.println(originalFilename);  
	    //xxx逻辑处理  
	    resultMap.put("result", "你好");  
	    return resultMap;  
  
    }    
	
}
